package com.minisoft.utils;

import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
import java.util.stream.*;

public class ServiceExtractor {

    public static void main1(String[] args) {
        String filePath = "C:\\Users\\31594\\IdeaProjects\\workUtils\\src\\main\\resources\\jps.txt"; // 文件路径
        List<String> serviceNames = new ArrayList<>();

        try {
            // 读取文件中的每一行
            List<String> lines = Files.readAllLines(Paths.get(filePath));

            for (String line : lines) {
                if (line.contains("gi-saas-sd-") && line.contains("-latest-boot.jar")) {
                    int start = line.indexOf("gi-saas-sd-") + "gi-saas-sd-".length();
                    int end = line.indexOf("-latest-boot.jar");
                    String serviceName = line.substring(start, end);
                    serviceNames.add(serviceName);
                }
            }

            // 对提取的服务名进行排序
            Collections.sort(serviceNames);

            // 输出排序后的服务名
            serviceNames.forEach(System.out::println);

        } catch (IOException e) {
            System.err.println("An error occurred while reading the file: " + e.getMessage());
        }
    }

    public static void main(String[] args) throws IOException {
        String filePath = "C:\\Users\\31594\\IdeaProjects\\workUtils\\src\\main\\resources\\jps.txt"; // 文件路径

        String directoryPath = "D:\\开盘系统\\HFXF-CORE-01脚本\\gicloud"; // 源目录路径
        List<String> lines = Files.readAllLines(Paths.get(filePath));
        String targetDirectory = "D:\\开盘系统\\HFXF-CORE-01脚本\\gicloud\\kf"; // kf目录路径

        moveFilesNotContainingName(directoryPath, lines, targetDirectory);
    }
    public static void moveFilesNotContainingName(String directoryPath, List<String> nameToCheck, String targetDirectory) {
        Path sourceDir = Paths.get(directoryPath);
        Path targetDir = Paths.get(targetDirectory);

        // 确保目标目录存在
        try {
            if (!Files.exists(targetDir)) {
                Files.createDirectories(targetDir);
            }
        } catch (IOException e) {
            System.err.println("Error creating target directory: " + e.getMessage());
            return;
        }

        try {
            Files.walkFileTree(sourceDir, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    String fileName = file.getFileName().toString();
                    //判断文件名是否包含nameToCheck中任意一个名称
                    if (nameToCheck.stream().anyMatch(fileName::contains)) {
                        return FileVisitResult.CONTINUE;
                    }
                    //不包含则移动到目标目录
                    Path targetPath = targetDir.resolve(sourceDir.relativize(file));
                    Files.move(file, targetPath, StandardCopyOption.REPLACE_EXISTING);
                    System.out.println("Moved: " + file + " to " + targetPath);
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (IOException e) {
            System.err.println("Error moving files: " + e.getMessage());
        }
    }
}
